package org.wishwork.api.stock.in.confirm;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.wishwork.api.Api;
import org.wishwork.api.stock.in.confirm.request.Item;
import org.wishwork.api.stock.in.confirm.request.StockInConfirmRequest;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 入库单确认
 * @author: WangYX
 * @create: 2022-04-11 20:21
 * @Version: 1.0.0
 **/

public class StockInConfirmApi extends Api<StockInConfirmResponse, StockInConfirmRequest> {

    private Context context;

    public StockInConfirmApi(Context context){
        this.context = context;
    }

    @Override
    protected String query(StockInConfirmRequest stockInConfirmRequest) throws Exception {
        String url = "/open/purchasein/received/upload";
        Map<String,Object> params = new HashMap<>();
        params.put("io_id",stockInConfirmRequest.getIoId());
        if (stockInConfirmRequest.getItems()!=null&&stockInConfirmRequest.getItems().size()>0) {
            List<Map<String,Object>> list = new ArrayList<>();
            for (Item item : stockInConfirmRequest.getItems()) {
                Map<String,Object> map = new HashMap<>();
                if (item.getQty()!=null) {
                    map.put("qty",item.getQty());
                }
                if (StrUtil.isNotEmpty(item.getSkuId())) {
                    map.put("sku_id",item.getSkuId());
                }
                list.add(map);
            }
            params.put("io_id",stockInConfirmRequest.getIoId());
        }
        return HttpHelper.sendRequest(params,url);
    }

    @Override
    protected StockInConfirmResponse process(JSONObject data, StockInConfirmRequest stockInConfirmRequest) throws Exception {
        return null;
    }
}
